package com.sky.service.user.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.exception.DataException;
import com.sky.mapper.common.DishMapper;
import com.sky.mapper.common.SetmealMapper;
import com.sky.mapper.common.ShoppingCartMapper;
import com.sky.pojo.Setmeal;
import com.sky.pojo.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.user.ShoppingCartService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional(readOnly = true)
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 新增购物车
     *
     * @param cartDTO
     * @return
     */
    @Override
    @Transactional(readOnly = false)
    public Result<String> addCart(ShoppingCartDTO cartDTO) {
        //1. 校验参数是否合法 不合法则抛出异常
        if(null == cartDTO || (null == cartDTO.getDishId() && null == cartDTO.getSetmealId())){
            throw new DataException(MessageConstant.DATA_ERROR);
        }

        //2. 如果传递过来的是dto 则创建pojo并进行数据的拷贝
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(cartDTO, shoppingCart);

        //3. 补齐没有拷贝成功的字段(时间 用户id等等)
        shoppingCart.setNumber(1);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart.setCreateTime(LocalDateTime.now());

        if(null != cartDTO.getDishId()){ //传递的是菜品
            DishVO dishVO = dishMapper.selectById(cartDTO.getDishId());
            shoppingCart.setName(dishVO.getName());
            shoppingCart.setImage(dishVO.getImage());
            shoppingCart.setAmount(dishVO.getPrice());
        }else{ //传递的是套餐
            Setmeal setmeal = setmealMapper.selectById(cartDTO.getSetmealId());
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setAmount(setmeal.getPrice());
        }

        //4. 通过用户id查询用户购物车中所有的数据
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.listByUserId(BaseContext.getCurrentId());

        //5. 判断当前新增是否在购物车中如果在则修改数据 如果不在则新增
        if(null == shoppingCartList || shoppingCartList.size() == 0){
            shoppingCartMapper.insert(shoppingCart);
            return Result.success();
        }

        for (ShoppingCart cart : shoppingCartList) {
            if((null != cart.getDishId() && cart.getDishId().equals(cartDTO.getDishId())) ||
                    (null != cart.getSetmealId() && cart.getSetmealId().equals(cartDTO.getSetmealId()))){
                cart.setNumber(cart.getNumber() + 1);
                shoppingCartMapper.updateNumberById(cart);
                return Result.success();
            }
        }

        //购物车里面有数据 但是我们添加的是一个新的菜品
        shoppingCartMapper.insert(shoppingCart);

        return Result.success();
    }

    /**
     * 查询当前登录人的购物车数据
     *
     * @return
     */
    @Override
    public Result<List<ShoppingCart>> list() {
        List<ShoppingCart> list = shoppingCartMapper.list(BaseContext.getCurrentId());
        return Result.success(list);
    }

    /**
     * 清空当前登陆人的购物车数据
     *
     * @return
     */
    @Override
    @Transactional(readOnly = false)
    public Result<String> clean() {
        shoppingCartMapper.clean(BaseContext.getCurrentId());
        return Result.success();
    }
}
